import 'package:edu/constants/app_images.dart';
import 'package:edu/pages/common/course_rating_summary_widget.dart';
import 'package:edu/pages/common/course_review_list_cell_widget.dart';
import 'package:edu/pages/course/course_detail/course_detail_page_logic.dart';
import 'package:edu/utils/account_info.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:tuple/tuple.dart';

class CourseDetailReview extends StatelessWidget {
  const CourseDetailReview({super.key});

  @override
  Widget build(BuildContext context) {
    return CustomScrollView(
      key: const PageStorageKey('review'),
      slivers: [
        SliverToBoxAdapter(child: const SizedBox(height: 10)),
        SliverToBoxAdapter(
          child: Builder(
            builder: (context) {
              final logic = context.read<CourseDetailPageLogic>();
              final summary = logic.courseDetail?.reviewSummary;
              if (summary != null) {
                return CourseRatingSummaryWidget(summary: summary);
              } else {
                return const SizedBox.shrink();
              }
            },
          ),
        ),
        const SliverToBoxAdapter(child: SizedBox(height: 20)),
        _buildMainBody(context),
        const SliverToBoxAdapter(child: SizedBox(height: 10)),
      ],
    );
  }

  Widget _buildMainBody(BuildContext context) {
    return Selector<CourseDetailPageLogic, bool>(
      selector: (context, logic) => logic.isReviewEmpty,
      builder: (context, v, child) {
        if (v) {
          return SliverToBoxAdapter(
            child: Center(
              child: Image.asset(AppImages.noContent, fit: BoxFit.cover),
            ),
          );
        } else {
          return Selector<CourseDetailPageLogic, Tuple2<int, bool>>(
            selector: (context, logic) =>
                Tuple2(logic.reviews.length, logic.canLoadMoreReviews),
            builder: (context, v, child) {
              final logic = context.read<CourseDetailPageLogic>();
              final canLoadMore = v.item2;
              final reviewList = logic.reviews;
              final itemCount = canLoadMore
                  ? reviewList.length + 1
                  : reviewList.length;
              return SliverList(
                delegate: SliverChildBuilderDelegate(childCount: itemCount, (
                  context,
                  index,
                ) {
                  final review = index < reviewList.length
                      ? reviewList[index]
                      : null;
                  final courseId = logic.courseId;
                  final isMycourse = logic.isMycourse;
                  if (review != null) {
                    return CourseReviewListCellWidget(
                      review: review,
                      courseId: courseId,
                      isMycourse: isMycourse,
                      isMyReview: review.userId == AccountInfo.shared.accountId,
                    );
                  } else {
                    return Center(
                      child: Padding(
                        padding: const EdgeInsets.all(10),
                        child: Builder(
                          builder: (context) {
                            Future.delayed(Duration.zero, () {
                              logic.loadReviews();
                            });
                            return const CircularProgressIndicator();
                          },
                        ),
                      ),
                    );
                  }
                }),
              );
            },
          );
        }
      },
    );
  }
}
